// This file is a part of Funky C++.
// Copyright (C) 2011, 2013  Valentin David
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "signal/function.hh"
#include "jack/client.hh"
#include <cmath>
#include "signal/sum.hh"
#include <bitset>
#include "player.hh"
#include "signal/fft.hh"
#include "signal/long_samples.hh"
#include "signal/complex.hh"
#include "signal/constant.hh"
#include "signal/sound_source.hh"
#include <random>
#include <iostream>
#include <chrono>
#include <thread>

int main() {
  using namespace funky;

  jack::client jc{"The thing!"};
  auto modulator = sound_source<512u>(jc, "modulator");
  auto signal = sound_source<512u>(jc, "signal");

  auto hann = function_signal<float(size_t)>
    {[] (size_t n) {
	return (1. - cos(2. * M_PI * n / 511.)) / 2.;
      }, 512u};

  auto modul_fft = dft(buffer<512u, 1024u>::to(modulator, hann));

  auto fir = buffer<512u, 1024u>::to(signal, hann);
  auto fft = dft(fir);

  auto ufft = dft(fft * modul_fft, true);

  auto ub = unbuffer<512u>::from(ufft);
  auto result = ::funky::real(ub);
  auto c = constant<float>(1./1024./8.);
  auto scaledown = c * result;

  auto p = make_player<512u>(scaledown, jc);

  std::this_thread::sleep_for(std::chrono::seconds(1));
  jc.activate();
  std::this_thread::sleep_for(std::chrono::seconds(100));

  return 0;
}
